package vip.zhenzicheng.concurrent_program.thread.sgg.lock;

import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 简单锁使用demo
 *
 * @author zhenzicheng
 * @date 2022-06-21 20:53
 */
public class SimpleLockDemo {

  ReentrantLock lock = new ReentrantLock();
  private AtomicInteger atomicInteger = new AtomicInteger();

  // 保证多个线程使用同一个lock对象前提下的悲观锁
  public void m2() {
    lock.lock();
    try {
      // 业务逻辑
    } finally {
      lock.unlock();
    }
  }

  // 悲观锁
  public synchronized void m1() {
    // 业务逻辑
  }

  // 乐观锁调用方式
  public void m3() {
    atomicInteger.incrementAndGet();
  }

}
